﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MediaDefinitionAuditRepositoryTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MediaDefinitionAuditRepositoryTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EFRepositoryTests
{
  using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data.Entity;
  using System.Linq;
  using System.Transactions;
  using BuildMeA;
  using DocProcessingRepository.Repositories;
  using Entities;
  using FluentAssertions;
  using NUnit.Framework;

  [Category("Integration")]
  [TestFixture]
  public class MediaDefinitionAuditRepositoryTests
  {
    private TransactionScope transactionScope;

    private MediaDefinitionAuditRepository auditRepository;
    private MediaDefinitionRepository mediaDefinitionRepository;
    private ApplicationUserRepository applicationUserRepository;

    private ApplicationUser user;
    private MediaDefinition mediaDefinition;

    [SetUp]
    public void Setup()
    {
      this.transactionScope = new TransactionScope();

      String connectionString = ConfigurationManager.ConnectionStrings["BBOS"].ConnectionString;

      this.auditRepository = new MediaDefinitionAuditRepository(connectionString);
      this.mediaDefinitionRepository = new MediaDefinitionRepository(connectionString);
      this.applicationUserRepository = new ApplicationUserRepository(connectionString);

      this.user = BuildMeA.ApplicationUser("user1", "John", "Doe");
      this.applicationUserRepository.Create(this.user);

      this.mediaDefinition = BuildMeA.MediaDefinition(1, 1, 1, 1, this.user.Id, "test", false, false);
      this.mediaDefinitionRepository.Create(this.mediaDefinition);
    }

    [TearDown]
    public void TearDown()
    {
      this.transactionScope.Dispose();
    }

    [Test]
    public void GivenAMediaDefId_WhenIAskForTheMediaDefinitionAudits_TheyAreReturnedFromTheDatabase()
    {
      MediaDefinitionAudit sampleAudit =
        BuildMeA.MediaDefinitionAudit(
          this.mediaDefinition.MediaDefID,
          this.user.Id,
          MediaDefinitionAudit.MediaDefinitionAuditType.MediaStockAdded,
          new DateTime(2015, 1, 1),
          "Details");

      this.auditRepository.Create(sampleAudit);

      List<MediaDefinitionAuditEntity> result = this.auditRepository.GetAudits(this.mediaDefinition.MediaDefID);
      result.Should().HaveCount(1);
      MediaDefinitionAuditEntity audit = result[0];

      audit.MediaDefinitionAuditId.Should().BeGreaterThan(0);
      audit.MediaDefinitionId.Should().Be(this.mediaDefinition.MediaDefID);
      audit.UserId.Should().Be(this.user.Id);
      audit.ManCoDocId.Should().Be(1);
      audit.IndexDefinitionId.Should().Be(1);
      audit.AuditType.Should().Be(MediaDefinitionAudit.MediaDefinitionAuditType.MediaStockAdded);
      audit.AuditDate.Should().Be(new DateTime(2015, 1, 1));
      audit.Details.Should().Be("Details");
      audit.Username.Should().Be("user1");
      audit.FirstName.Should().Be("John");
      audit.LastName.Should().Be("Doe");
      audit.QueueName.Should().Be("test");
    }

    [Test]
    public void GivenValidData_WhenIAddAMediaDefinitionAudit_TheyAreAddedToTheDatabase()
    {
      MediaDefinitionAuditEntity addedAudit =
        this.auditRepository.AddAudit(this.mediaDefinition.MediaDefID,
          this.user.Id,
          MediaDefinitionAudit.MediaDefinitionAuditType.MediaStockStationNumbersUpdated,
          new DateTime(2015, 1, 1), "Details");

      addedAudit.Should().NotBeNull();
      addedAudit.MediaDefinitionAuditId.Should().BeGreaterThan(0);
      addedAudit.MediaDefinitionId.Should().Be(this.mediaDefinition.MediaDefID);
      addedAudit.UserId.Should().Be(this.user.Id);
      addedAudit.ManCoDocId.Should().Be(1);
      addedAudit.IndexDefinitionId.Should().Be(1);
      addedAudit.AuditType.Should().Be(MediaDefinitionAudit.MediaDefinitionAuditType.MediaStockStationNumbersUpdated);
      addedAudit.AuditDate.Should().Be(new DateTime(2015, 1, 1));
      addedAudit.Details.Should().Be("Details");
      addedAudit.Username.Should().Be("user1");
      addedAudit.FirstName.Should().Be("John");
      addedAudit.LastName.Should().Be("Doe");
      addedAudit.QueueName.Should().Be("test");
    }
  }
}
